package com.joysuch.wwyt.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.core.entity.BaseManageType;
import com.joysuch.wwyt.core.entity.BaseManageTypeVo;
import com.joysuch.wwyt.core.entity.BaseTypeGroupItem;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.mapper.BaseManageTypeMapper;
import com.joysuch.wwyt.core.repository.BaseTypeGroupItemDao;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BaseManageTypeService;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.edu.entity.EduCertificateNotify;
import com.joysuch.wwyt.edu.mapper.EduCertificateNotifyMapper;
import com.joysuch.wwyt.util.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @author : pp
 * @date : Created in 2021/4/14 10:29
 */
@Service
@Transactional
public class BaseManageTypeServiceImpl  extends ServiceImpl<BaseManageTypeMapper, BaseManageType> implements BaseManageTypeService {
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private BaseTypeGroupItemDao itemDao;

    @Override
    public void add(BaseManageType entity) {
        //一个组织下只能有唯一一种管理类型
        BaseManageType repeat = baseMapper.checkRepeat(entity.getDepartId(), entity.getManageTypeDictionaryCode());
        if (repeat != null) {
            throw new IllegalArgumentException("该组织已存在此管理类型!");
        }
        entity.setOrgCode(Context.getCompanyCode());
        entity.setTenentId(Context.getCurrentTenantId());
        baseMapper.insert(entity);
    }

    @Override
    public PageInfo<BaseManageTypeVo> findAll(Long departId, Integer pageNum, Integer pageSize) {
        PageInfo<BaseManageTypeVo> pageInfo = new PageInfo<>(pageNum, pageSize);
        Page<BaseManageType> page = new Page<>(pageNum, pageSize);

        IPage<BaseManageType> historyIPage = baseMapper.selectByDepartId(page, departId);
        List<BaseManageType> results = historyIPage.getRecords();
        List<BaseManageTypeVo> list = results.stream().map(e -> {
            Optional<BaseUser> byId = baseUserDao.findById(e.getUserId());
            if (byId.isPresent()) {
                BaseUser baseUser = byId.get();
                BaseManageTypeVo vo = new BaseManageTypeVo();
                BeanUtils.copyProperties(e, vo);
                vo.setUserCode(baseUser.getCode());
                vo.setUserName(baseUser.getRealName());
                Optional<BaseTypeGroupItem> item = itemDao.findById(e.getManageTypeDictionaryCode());
                if (item.isPresent()) {
                    BaseTypeGroupItem baseTypeGroupItem = item.get();
                    vo.setManageTypeName(baseTypeGroupItem.getName());
                }
                return vo;
            } else {
                return null;
            }
        }).collect(Collectors.toList());
        pageInfo.setLists(list);
        pageInfo.setTotalCount((int) historyIPage.getTotal());
        return pageInfo;
    }

    @Override
    public void delete(List<Long> ids) {
        baseMapper.deleteBatch(ids);
    }

    @Override
    public List<BaseManageTypeVo> getAll(Long departId) {
        List<BaseManageType> types = baseMapper.findByDepartId(departId);
        List<BaseManageTypeVo> list = types.stream().map(e -> {
            Optional<BaseUser> byId = baseUserDao.findById(e.getUserId());
            if (byId.isPresent()) {
                BaseUser baseUser = byId.get();
                BaseManageTypeVo vo = new BaseManageTypeVo();
                BeanUtils.copyProperties(e, vo);
                vo.setUserCode(baseUser.getCode());
                vo.setUserName(baseUser.getRealName());
                Optional<BaseTypeGroupItem> item = itemDao.findById(e.getManageTypeDictionaryCode());
                if (item.isPresent()) {
                    BaseTypeGroupItem baseTypeGroupItem = item.get();
                    vo.setManageTypeName(baseTypeGroupItem.getName());
                }
                return vo;
            } else {
                return null;
            }
        }).collect(Collectors.toList());
        return list;
    }
}
